﻿@{
    ViewData["Title"] = "MeasureUnitAddView";
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<h1>新增单位</h1>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">单位编码</label>
        <div class="layui-input-block">
            <input type="text" id="unitCode" name="unitCode" lay-verify="required" placeholder="请输入单位编码" autocomplete="off" class="layui-input">
            @* <button type="button" class="layui-btn layui-bg-orange" id="generateId">生成雪花 ID</button> *@
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单位名称</label>
        <div class="layui-input-block">
            <input type="text" id="unitName" name="unitName" lay-verify="required" placeholder="请输入单位名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否是主单位</label>
        <div class="layui-input-block">
            <input type="radio" id="isNoUnit" name="isNoUnit" value="1" title="是" checked lay-filter="isNoUnit">
            <input type="radio" id="isNoUnit" name="isNoUnit" value="0" title="否" lay-filter="isNoUnit">
        </div>
    </div>




    <!-- 主单位相关项（动态添加部分） -->
    <div id="mainUnitFields" style="display:none;">
        <div class="layui-form-item">
            <label class="layui-form-label">主单位</label>
            <div class="layui-input-block">
                <select id="primaryUnit" name="primaryUnit" lay-search>
                    <option value="0">请选择主单位</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">与主单位换算比例</label>
            <div class="layui-input-block">
                <input type="text" id="conversion" name="conversion" placeholder="请输入换算比例（默认一比多少）" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="radio" id="isUsed" name="isUsed" value="1" title="是" checked>
            <input type="radio" id="isUsed" name="isUsed" value="0" title="否">
        </div>
    </div>


    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" id="remark" name="remark"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
        </div>
    </div>
</form>

<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>
<script>
       // 页面加载完成后执行的函数
    $(function () {
        $('#primaryUnit').val('1');
        $('#conversion').val('1');
        getMainUnitList()
    });


    //获取主单位列表
    function getMainUnitList() {
        $.ajax({
            url: '/mainData/getPrimaryUnit',
            type: 'get',
            dataType: 'json',
            success: function (res) {
        console.log(res.data.result);
                if (res.code == 0) {

                var select = $('#primaryUnit'); // 获取 select 元素

                // 清空现有的选项（除了第一个默认选项）
                select.find('option:not(:first)').remove();
                console.log(res.data);
                // 动态添加选项
            $.each(res.data, function (key, value) {
                    select.append('<option value="' + value.id + '">' + value.unitName + '</option>');
                });
                layui.form.render('select');

                }
            }
        })

    }



    layui.use(['form', 'laydate', 'util'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var util = layui.util;

       // 监听是否主单位的选择
       form.on('radio(isNoUnit)', function (data) {
       var value = data.value; // 获取选中的值
       console.log(value);
       if (value === '1') {
            // 如果是主单位，隐藏额外的字段
            $('#mainUnitFields').hide();
        $('#conversionRatio').val("1");
        $('#conversion').val("1");

       }
        else {
            // 如果不是主单位，显示额外的字段
            $('#mainUnitFields').show();
            $('#conversionRatio').val("");
            $('#conversion').val("");
        }
    });


      // 提交事件
      form.on('submit(demo1)', function(data){
          console.log(data);



        var field = data.field; // 获取表单字段值

        if (!isNaN(parseFloat(field.conversionValue))) {
            layer.alert('请输入有效的换算比例哦！亲', {
                skin: 'layui-layer-molv' //样式类名
              });
            return false; 阻止表单跳转

        };

            // alert("请输入有效的换算比例！");
            // console.log("请输入有效的数字！");
            // layer.msg('请输入有效的数字！', { icon: 5 }); 弹出提示
            // return; 阻止后续执行

        console.log(field);
        field.isNoUnit=parseInt(field.isNoUnit);
        field.primaryUnit=parseInt(field.primaryUnit);
         field.isUsed=parseInt(field.isUsed);
         field.conversion=parseInt(field.conversion);

        $.ajax({
            url:'/mainData/measureUnitAdd',
            type:'post',
            data:field,
            dataType:'json',
            success:function(res){
                console.log(res);
            if(res.code==1){
                      layer.msg(res.msg,{icon:1,time:1000,title:'OK了,老铁'},function(){

                       closeWorkstation();
                       });
                }
                    if(res.code==-100){
                     layer.alert('已经有次单位名称！')

                }
            }
        })
        return false; // 阻止默认 form 跳转
      });
    });


    //关闭弹框
    function closeWorkstation() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前的索引
        parent.layer.close(index); //再执行关闭
    }

</script>